import { Card, message, Row, Col, Space } from 'antd';
import { history } from 'umi';
import ProForm, {
  ProFormDatePicker,
  ProFormUploadButton,
  ProFormText,
  ProFormSelect,
} from '@ant-design/pro-form';
import { useRequest } from 'umi';
import type { FC } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import {
  diseaseList,
} from '@/services/ant-design-pro/api';
import {
  fakeSubmit,
  queryArticleDetail,
  update,
  fakeSubmitNotice,
  queryDeptList,
  queryArticleDepts,
  updateArticleDeptIds,
} from './service-science';
import RichText from '@/components/RichText';

const BasicForm: FC<Record<string, any>> = () => {
  const { run } = useRequest(fakeSubmit, {
    manual: true,
    onSuccess: () => {
      message.success('提交成功');
      history.push('/article/article-list');
    },
  });

  // const { data, loading } = useRequest(() => {
  //   if (history.location.query.id) {
  //     return queryArticleDetail(history.location.query);
  //   }
  // });

  const onFinish = async (values: Record<string, any>) => {
    // let diseaseIds = [];
    // let diseaseTags = '';
    console.log('values', values);
    if (values.upload.length > 0) {
      if (values.upload[0].url) {
        let arr = values.upload[0].url.split('http://oss.yizuoyiyou.com.cn');
        values.cover = arr[1];
      }
      if (values.upload[0].response) {
        values.cover = values.upload[0].response.data.url;
      }
    }
    values.channel = history.location.query.channel;

    if (history.location.query.id) {
      values.id = history.location.query.id;
    }
    try {
      const response = await update(values);
      if (response.code == 200) {
        message.success('操作成功');
        history.push('/content/doctor-creation-manager');
      } else {
        message.error('操作失败!');
      }
    } catch (error) {
      message.error(error);
      console.log(error);
    }
  };

  const formItemLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
  };

  return (
    <PageContainer content="">
      <Card bordered={false}>
        <ProForm
          hideRequiredMark
          style={{ margin: 'auto', marginTop: 8 }}
          name="basic"
          layout="horizontal"
          // initialValues={data}
          onFinish={onFinish}
          {...formItemLayout}
          request={async () => {
            if (history.location.query.id) {
              const res = await queryArticleDetail(history.location.query);
              if (res.code == 200) {
                let data = res.data;
                if (data.cover) {
                  let upload = [
                    {
                      uid: '1',
                      name: 'icon',
                      status: 'done',
                      url: 'http://oss.yizuoyiyou.com.cn' + data.cover,
                    },
                  ];
                  data.upload = upload;
                }
                data.addUserType = data.addUserType + '';
                data.channel = data.channel + '';
                const response = await queryArticleDepts({ id: history.location.query.scienceId });

                if (response.code == 200) {
                  console.log('response', response);
                  if (response.data != null) {
                    let deptIds = [];
                    for (var i = 0; i < response.data.length; i++) {
                      deptIds.push(response.data[i].id);
                    }
                    data.deptIds = deptIds;
                  }
                }
                console.log('data------', data);
                return data;
              }
            } else {
              return {};
            }
          }}
          submitter={{
            render: (props, doms) => {
              return (
                <Row>
                  <Col span={14} offset={4}>
                    <Space>{doms}</Space>
                  </Col>
                </Row>
              );
            },
          }}
        >
          <ProFormText
            width="md"
            label="标题"
            name="title"
            rules={[
              {
                required: true,
                message: '请输入标题',
              },
            ]}
            placeholder="请输入标题"
          />
          {/* <ProFormText
            width="md"
            label="副标题"
            name="subTitle"
            rules={[
              {
                required: true,
                message: '请输入副标题',
              },
            ]}
            placeholder="请输入文章副标题"
          /> */}
          {/* <ProFormSelect
            width="md"
            name="diseases"
            label="疾病标签"
            showSearch
            fieldProps={{
              mode: 'multiple',
              labelInValue: true,
            }}
            request={async () => {
              return queryDiseaseTags().then(({ data }) => {
                return data.map((item) => {
                  return {
                    label: item.name,
                    value: item.id,
                  };
                });
              });
            }}
          /> */}
          <ProForm.Item label="正文内容" name="content">
            <RichText />
          </ProForm.Item>
          <ProFormUploadButton
            name="upload"
            label="封面"
            max={1}
            fieldProps={{
              name: 'file',
              listType: 'picture-card',
            }}
            action="/api/file/upload"
            rules={[
              {
                required: true,
                message: '请上传封面',
              },
            ]}
            extra="封面将在列表页显示"
          />
          <ProFormSelect
            width="md"
            name="diseaseIds"
            label="疾病"
            fieldProps={{
              mode: 'multiple',
            }}
            showSearch
            request={async ({ keyWords }) => {
              let query = {
                likeQuery: {
                  name: keyWords,
                },
                pageNum: 1,
                pageSize: 20,
              };
              return diseaseList(query).then(({ data }) => {
                return data.list.map((item) => {
                  return {
                    label: item.name,
                    value: item.id,
                  };
                });
              });
            }}
            extra="选填，请选择和科普文章相关的疾病标签，方便用户查询阅读"
          />
          <ProFormText
            width="md"
            label="作者id"
            name="doctorId"
            rules={[
              {
                required: true,
                message: '请输入作者id',
              },
            ]}
            placeholder="请输入作者id"
          />
          {/* 
           <ProFormRadio.Group
            options={[
              {
                value: '0',
                label: '用户',
              },
              {
                value: '1',
                label: '医生',
              },
              {
                value: '2',
                label: '系统',
              },
            ]}
            label="创作人"
            name="addUserType"
          /> */}
          {/* <ProFormText
            width="md"
            label="用户id"
            name="addUserId"
            rules={[
              {
                required: true,
                message: '请输入用户id',
              },
            ]}
            placeholder="请输入用户id"
          /> 
          <ProFormDatePicker width="md" name="publishTime" label="创作日期" />*/}
        </ProForm>
      </Card>
    </PageContainer>
  );
};

export default BasicForm;
